# %%
#coding=utf-8

from selenium import webdriver
import tkinter as ck
from tkinter import ttk
import requests
import threading


#创建窗口
gjcss = ck.Tk()
gjcss.title("网站运行检测")
gjcss.geometry('600x350')

options = webdriver.ChromeOptions() # 设置中文
options.add_argument('lang=zh_CN.UTF-8') # 更换头部
options.add_argument('User-Agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"')# 请求响应标头


#URL文本
li1 = ck.Label(text="请输入文件名:", font=('Arial', 12)) 
li1.place(x=0, y=10)

#输入框
srk = ck.Entry(bd=2,width=30)
srk.insert(0,'wyurl.txt') 
srk.place(x=110, y=10)


#浏览器驱动
li2 = ck.Label(text="浏览器驱动:", font=('Arial', 12)) 
li2.place(x=0, y=40)

#输入框
srk2 = ck.Entry(bd=2,width=30)
srk2.place(x=110, y=40)


#创建表格
columns = ['序号', '网站名称', '网址','运行情况']
liebiao = ttk.Treeview(gjcss,columns=columns,show='headings')
liebiao.heading('序号', text='序号')
liebiao.heading('网站名称', text='网站名称')
liebiao.heading('网址', text='网址')
liebiao.heading('运行情况', text='运行情况')

liebiao.column('序号',width=50,anchor='s')
liebiao.column('网站名称',width=200,anchor='s')
liebiao.column('网址',width=200,anchor='s')
liebiao.column('运行情况',width=100,anchor='s')

# global i
# i = 0

def lqcz():
    global i
    i = 0
    
    getssc = srk.get()
    browsedriver = srk2.get()

    #打开谷歌浏览器
    #谷歌浏览器：C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chromedriver.exe
    # browser = webdriver.Chrome(r'%s' % (browsedriver),chrome_options=options)
    browser = webdriver.Chrome(r"C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chromedriver.exe",chrome_options=options)
    with open(getssc,"r",encoding='utf8') as f:#打开记事本
        for line in f.readlines():
            data = line.strip('\n') #去掉换行符
            try:
                browser.get(data)   #打开网址
                titlbt = browser.title  #获取网站标题
                weburl = requests.get(data)
                yxcode = weburl.status_code #获取状态码
                if  yxcode==200:
                    condition = '运行正常'
                else:
                    condition = '运行异常'  
                
            except:
                print ("Error:此无法网站打开",data)
                titlbt = '网站异常'
                condition = '运行异常'
            i += 1

            liebiao.insert('',0,text='date0',values=(i,titlbt,data,condition)) #向表格插入数据
#线程
def xc():            
    t = threading.Thread(target=lqcz)
    t.start()              
    # time1.sleep(10)

#wyurl.txt
antj = ck.Button(text="提交",width=10,command=xc)
antj.place(x=330, y=5)




liebiao.place(x=10, y=80)   #表格插入

vsb = ttk.Scrollbar(gjcss, orient="vertical", command=liebiao.yview)#Y轴滚动条
vsb.place(x=545, y=81, height=225)
liebiao.configure(yscrollcommand=vsb.set)



gjcss.mainloop()

